package com.mb.ext.core.util;

import com.alibaba.fastjson.JSON;
import com.mb.ext.core.constant.WechatConstants;
import com.mb.ext.msg.HttpClientHelper;
import com.mb.ext.msg.WXJSONAccessToken;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.*;

@Component
public class OfficialAccessTokenManager {
    private static OfficialAccessTokenManager.WXTokenInfo wxToken;

    /**
     * 微信TOKEN信息
     *
     * @author qiaojianhui
     *
     */
    public static class WXTokenInfo {
        private String token;
        private long timestamp;
        private int expiresin;

        /**
         * @return the token
         */
        public String getToken() {
            return token;
        }

        /**
         * @param token
         *            the token to set
         */
        public void setToken(String token) {
            this.token = token;
        }

        /**
         * @return the timestamp
         */
        public long getTimestamp() {
            return timestamp;
        }

        /**
         * @param timestamp
         *            the timestamp to set
         */
        public void setTimestamp(long timestamp) {
            this.timestamp = timestamp;
        }

        public int getExpiresin() {
            return expiresin;
        }

        public void setExpiresin(int expiresin) {
            this.expiresin = expiresin;
        }

    }

    /**
     * 得到当前有效的Token值
     *
     * @return
     */
    public String getEffectiveToken(String appId, String appSecret) {
        if (wxToken == null) {
            WXJSONAccessToken token = parseWXJSONAccessToken(appId, appSecret);
            return token.getAccess_token();
        }

        long timestamp = new Date().getTime();
        int interval = wxToken.expiresin * 1000; // timestamp是毫秒为单位
        // token中的时间以秒为单位  1秒=1000毫秒

        if (interval + wxToken.timestamp > timestamp) {
            return wxToken.getToken();
        } else {
            WXJSONAccessToken token = parseWXJSONAccessToken(appId, appSecret);
            return token.getAccess_token();
        }
    }

    /**
     * 解析AccessToken
     *
     * @return
     */
    public WXJSONAccessToken parseWXJSONAccessToken(String appId, String appSecret) {
        wxToken = new OfficialAccessTokenManager.WXTokenInfo();
        wxToken.timestamp = new Date().getTime();

        String accessToken = getAccessToken(appId,appSecret);

        WXJSONAccessToken token = JSON.parseObject(accessToken,
                WXJSONAccessToken.class);
        wxToken.expiresin = token.getExpires_in();
        wxToken.token = token.getAccess_token();

        return token;

    }

    /**
     * 获取AccessToken
     *
     * @return
     */
    public String getAccessToken(String appId, String appSecret) {
        // String token="";
//		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
//				+ WXServiceInfo.APPID + "&secret=" + WXServiceInfo.APPSECRET;
        String r = HttpClientHelper.sendGet(WechatConstants.URL_ACCESSTOKEN.replace("APP_ID", appId).replace("APP_SECRET", appSecret), null, "UTF-8");

        return r;
    }

    
}
